package com.donleo.student.controller;


import com.donleo.common.api.CommonResult;
import com.donleo.common.model.Team;
import com.donleo.student.service.TeamService;
import com.donleo.student.vo.TeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 小组表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @PostMapping("/add")
    public CommonResult add(@RequestBody Team team) {
        return teamService.add(team);
    }

    @GetMapping("/delete")
    public CommonResult delete(Integer id) {
        return teamService.delete(id);
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody Team team) {
        Assert.notNull(team.getId(), "Id is not to be null");
        return teamService.updateTeam(team);
    }

    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        Team team = teamService.findById(id);
        return CommonResult.success(team);
    }

    @GetMapping("/findAll")
    public CommonResult findAll(TeamVo teamVo) {
        List<Team> list = teamService.findAll(teamVo);
        long total = teamService.getCount(teamVo);
        return CommonResult.success(total, list);
    }

    /**
     * 分配组长
     *
     * @param team
     * @return
     */
    @PostMapping("/distributeLeader")
    public CommonResult distributeLeader(@RequestBody Team team) {
        Assert.notNull(team.getId(), "Id is not to be null");
        return teamService.distributeLeader(team);
    }

    /**
     * 分配组员
     *
     * @param teamCode 小组编号
     * @param codes    用户编号集合
     * @return
     */
    @PostMapping("/distributeMembers")
    public CommonResult distributeMembers(@RequestParam("teamCode") String teamCode,
                                          @RequestParam(value = "codes", required = false, defaultValue = "[]") String[] codes) {
        List<String> strings = Arrays.asList(codes);
        return teamService.distributeMembers(teamCode, strings);
    }

}
